package com.yimixian.app.ui;

import android.os.Handler;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.yimixian.app.R;
import com.yimixian.app.activity.BaseNaviFragmentGroupActivity;
import com.yimixian.app.util.ThreadManager;
import com.ymx.sdk.util.ViewUtils;

public abstract class LoadingPage extends FrameLayout {

	public static final int STATE_UNLOADED = 0;

	public static final int STATE_LOADING = 1;

	public static final int STATE_ERROR = 3;

	public static final int STATE_EMPTY = 4;

	public static final int STATE_SUCCEED = 5;

	private View mLoadingView;

	private View mErrorView;

	private View mEmptyView;

	private View mSucceedView;

	public int mState;

	private LoadType mLoadType;

	private BaseNaviFragmentGroupActivity mActivity;

	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			setPageVisiableSafe();

		};
	};

	public LoadingPage(BaseNaviFragmentGroupActivity activity) {
		super(activity);
		mActivity = activity;
		init(null);
	}

	public LoadingPage(BaseNaviFragmentGroupActivity activity, View emptyView) {
		super(activity);
		mActivity = activity;
		init(emptyView);
	}

	private void init(View emptyView) {
		setBackgroundColor(getResources().getColor(R.color.bg_page));
		mState = STATE_UNLOADED;
		mLoadingView = createLoadingView();
		if (null != mLoadingView) {
			addView(mLoadingView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		}

		mErrorView = createErrorView();
		if (null != mErrorView) {
			addView(mErrorView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		}

		// mEmptyView = createEmptyView(emptyResId);
		mEmptyView = createEmptyView(emptyView);
		if (null != mEmptyView) {
			addView(mEmptyView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		}
		setPageVisiableSafe();
	}

	private void setPageVisiableSafe() {
		long currentThreadId = Thread.currentThread().getId();
		long mainThreadId = mActivity.getMainLooper().getThread().getId();
		if (currentThreadId == mainThreadId) {
			setPageVisiable();
		} else {
			mActivity.post(new Runnable() {

				@Override
				public void run() {
					setPageVisiable();
				}
			});
		}
	}

	private void setPageVisiable() {
		if (null != mLoadingView) {
			mLoadingView.setVisibility(mState == STATE_UNLOADED || mState == STATE_LOADING ? View.VISIBLE : View.GONE);
		}
		if (null != mErrorView) {
			mErrorView.setVisibility(mState == STATE_ERROR ? View.VISIBLE : View.GONE);
		}
		if (null != mEmptyView) {
			mEmptyView.setVisibility(mState == STATE_EMPTY ? View.VISIBLE : View.GONE);
		}
		if (mState == STATE_SUCCEED) {
			if (mSucceedView == null) {
				mSucceedView = createLoadedView();
				ViewUtils.removeSelfFromParent(mSucceedView);
				addView(mSucceedView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			}
			mSucceedView.setVisibility(View.VISIBLE);
		} else if (null != mSucceedView) {
			mSucceedView.setVisibility(View.GONE);
		}
	}

	public void reset() {
		setState(STATE_UNLOADED);
	}

	protected boolean needReset() {
		return mState == STATE_ERROR || mState == STATE_EMPTY;
	}

	public synchronized void setState(int state) {
		if (state < STATE_UNLOADED || state > STATE_SUCCEED) {
			return;
		}
		mState = state;
		setPageVisiableSafe();
	}

	// 直接显示 不重新加载
	public synchronized void show() {
		mLoadType = LoadType.ASYN;
		if (needReset()) {
			mState = STATE_UNLOADED;
		}
		if (mState == STATE_UNLOADED) {
			mState = STATE_LOADING;
			load();
//			LoadingTask task = new LoadingTask();
//			ThreadManager.getLongPool().execute(task);
		}
		setPageVisiableSafe();
	}

	// 重新加载布局和数据
	public synchronized void showLoad(boolean isLoadView) {
		if (isLoadView) {
			mSucceedView = null;
		}
		mState = STATE_UNLOADED;
		show();
	}

	// 重新加载数据
	public synchronized void showLoad() {
		showLoad(false);
	}

	public synchronized void showMain() {
		mLoadType = LoadType.SYN;
		if (needReset()) {
			mState = STATE_UNLOADED;
		}
		if (mState == STATE_UNLOADED) {
			mState = STATE_LOADING;
		}
		setPageVisiableSafe();
		if (mState != STATE_SUCCEED) {
			loadMain();
		}
	}

	protected View createLoadingView() {
		return LayoutInflater.from(mActivity).inflate(R.layout.loading_page_loading, null);
	}

	public View createEmptyView(View emptyView) {
		if (emptyView == null) {
			FrameLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT, Gravity.CENTER);
			emptyView = LayoutInflater.from(mActivity).
					inflate(R.layout.loading_page_empty, null);
			emptyView.setLayoutParams(params);
			return emptyView;
		}else{
			return emptyView;
		}
//		FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(mActivity).
//				inflate(R.layout.loading_page_empty, null);
//		if (emptyView == null) {
////			ImageView imageView = new ImageView(getContext());
//			TextView textView = new TextView(getContext());
//			textView.setText("暂无数据");
//			textView.setTextSize(18);
////			int wh = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 100f, getResources().getDisplayMetrics());
//			FrameLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
////			imageView.setScaleType(ScaleType.CENTER_INSIDE);
////			imageView.setImageResource(R.drawable.ic_empty_page);
//			frameLayout.addView(textView, params);
//		} else {
//			FrameLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
//			frameLayout.addView(emptyView, params);
//		}
//		return frameLayout;
	}

	protected View createErrorView() {
		View view = LayoutInflater.from(mActivity).inflate(R.layout.loading_page_error, null);
		view.findViewById(R.id.paper_button).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mLoadType.equals(LoadType.ASYN)) {
					show();
				} else {
					showMain();
				}
			}
		});
		return view;
	}

	public abstract View createLoadedView();

	public abstract void load();

	public abstract void loadMain();

	public void setLoadResult(LoadResult result){
		int state;
		if (result == LoadResult.ERROR) {
			state = STATE_ERROR;
		} else if (result == LoadResult.EMPTY) {
			state = STATE_EMPTY;
		} else {
			state = STATE_SUCCEED;
		}
		setState(state);
	}

//	class LoadingTask implements Runnable {
//
//		@Override
//		public void run() {
//			LoadResult result = load();
//			int state;
//			if (result == LoadResult.ERROR) {
//				state = STATE_ERROR;
//			} else if (result == LoadResult.EMPTY) {
//				state = STATE_EMPTY;
//			} else {
//				state = STATE_SUCCEED;
//			}
//			setState(state);
//		}
//	}

	public enum LoadResult {
		ERROR, EMPTY, SUCCEED
	}

	// 访问类型（同步，异步）
	public enum LoadType {
		ASYN, SYN
	}
}
